<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="/client/backgroundManagement/commons/global.jsp" %>

<script>
	var path = "/sddc/client/";
	console.log(path)
	var dojoConfig = {
		serverIP : location.host,
		gfxRenderer : "svg,canvas,vml",
		packages : [ {
			name : 'app',
			location : path + 'app'
		}, {
			name : 'gistech',
			location : path + 'gistech'
		} ]
	};
</script>
<script type="text/javascript"
	src="/arcgis_js_api/v313/library/3.13/init.js"></script>
<script type="text/javascript">
	require([ "dojo/parser","dojo/_base/lang", "dojo/on",  "gistech/util/ajaxUtil", "dijit/dijit", "esri/tasks/QueryTask","esri/tasks/query", "dojo/topic","dojo/ready" ], function(
			Parser,lang,on,ajaxUtil, dijit,QueryTask,query,topic, ready) {
		     var resultLayer ='' ; 
		     
		    
		     
 			$('#roleAddForm').form({
	            url : '${path }/serverController/add',
	            onSubmit : function() {
	            	 
	                progressLoad();
	                var isValid = $(this).form('validate');
	                if (!isValid) {
	                    progressClose();
	                }
 					
	        	 return isValid;
	            },
	            
	            success : function(result) {
	            	 topic.subscribe("updateLayerTable", lang.hitch(this, this.updateLayerTable_));
	                progressClose();
	                result = $.parseJSON(result);
	                //保存成功
	                if (result.success) {
	                	debugger
	                	var serverInfoId = result.msg ;
		                //验证动态URL
		                var dynamicUrl =   dojo.byId('dynamicsUrl').value; 
	        			var queryTask=new QueryTask(dynamicUrl);
	       			    query = new esri.tasks.Query();
					 
			       			 queryTask.execute(query, function(resp){
			       				 
				       			    if(resp.layers.length !=0 ){ 
				       			         isValid=true;
				       			         resultLayer ='' ; 
					       			      for(var i=0;i<resp.layers.length;i++){ 
					       			    	  
					       			        resultLayer += (resp.layers[i].name+'('+resp.layers[i].id+')|');
					       			        
					       			      }
					       			      
					       			      // var tableNames = this.getTableNames(resp.layers.length);
					       			      
					  					var dynamicUrl =   dojo.byId('dynamicsUrl').value; 

					       			      
					       			       var params = "serverInfoId="+serverInfoId+"&layerInfo="+resultLayer;
					       			   		//保存图层信息
						                	ajaxUtil.request("${path }/layerInfoController/add",
						                			params,lang.hitch(this,function(result){
	 												if(result.success){
	 													
	 													//更新图层中的表名
	 													for(var i = 0 ;i<resp.layers.length;i++){
	 														var index = i;
	 														var getUrl = dynamicUrl+ '/exts/SDDCMapSOE/GetLayerInfo?f=json&LayerIndex='+index;
	 														topic.publish("updateLayerTable", getUrl,serverInfoId,index);
	 														/*  ajaxUtil.request(getUrl,"",lang.hitch(this,function(result){
	 									           	    	 	    debugger;
	 									           	    	 	   console.log(result);
	 									           	    	 	   
	 									           	    	 	ajaxUtil.request("${path }/layerInfoController/editByServerId","serverInfoId="+serverInfoId+"&layerIndex="+index+"&tableName="+result.name,
	 									           	    	 			lang.hitch(this,function(result){
	 									           	    	 		
	 									           	    	 	}));
	 									           	    	 	
	 									        	    	})); */
	 									        	    	
	 													}
	 													
	 													 parent.$.modalDialog.openner_dataGrid.datagrid('reload'); 
	 								                     parent.$.modalDialog.handler.dialog('close');
	 												}else {
	 							 	                    parent.$.messager.alert('错误', "", 'error');
	 							 	                }
						        	    	}));
						                	
				       			    }else{
				       			    	progressClose();
				       			        alert('服务下无图层,请确认动态服务URL无误');
				       			    }
				       			 
				       		 },function(error){
								 progressClose();
								 alert('读取服务出错,请确认动态服务URL无误');
								 //删除
				                	ajaxUtil.request("${path }/serverController/delete",
				                			"id="+serverInfoId,lang.hitch(this,function(result){
				           	    	 
				        	    	}));
				       			 
				       		 } )
						  

	                } else {
	                    parent.$.messager.alert('错误', result.msg, 'error');
	                }
	            },
	            
	            updateLayerTable_: function(getUrl,serverInfoId,index){
	            	ajaxUtil.request(getUrl,"",lang.hitch(this,function(result){
        	    	 	    debugger;
        	    	 	   //console.log(result);
        	    	 	   
        	    	 	ajaxUtil.request("${path }/layerInfoController/editByServerId","serverInfoId="+serverInfoId+"&layerIndex="+index+"&tableName="+result.name.replace(/%/g,""),
        	    	 			lang.hitch(this,function(result){
        	    	 		
        	    	 	}));
        	    	 	
        	    	 	
     	    	}));
					//alert(8989+getUrl)
				}
	        }); 
			
	        
	        
			function getTableNames(layerCount){
       			  
				
				var dynamicUrl =   dojo.byId('dynamicsUrl').value; 
				for(var i = 0 ;i<layerCount;i++){
					var getUrl = dynamicUrl+ '/exts/SDDCMapSOE/GetLayerInfo?f=json&LayerIndex='+index;
                	ajaxUtil.request(getUrl,
                			"",lang.hitch(this,function(result){
           	    	 	   //debugger;
           	    	 	   console.log(result);
        	    	}));
					
				}
				
				
       			  
			}
			
			
			function checkFlag2(isValid){
				debugger;
       			if(isValid){
       				isValid=false;
	                //验证动态URL
	                var dynamicUrl =   dojo.byId('dynamicsUrl').value; 
        			var queryTask=new QueryTask(dynamicUrl);
       			    query = new esri.tasks.Query();
					try{
		       			 queryTask.execute(query, function(resp){
		       				 
			       			    if(resp.layers.length !=0 ){ 
			       			      isValid=true;
			       			         resultLayer ='' ; 
				       			      for(var i=0;i<resp.layers.length;i++){ 
				       			    	  //debugger;
				       			       resultLayer += (resp.layers[i].name+'('+resp.layers[i].id+')|')
				       			      }
			       			    }else{
			       			    	progressClose();
			       			     alert('服务下无图层,请确认动态服务URL无误');
			       			     
			       			    }
			       			 return isValid;
			       		 })
					}catch(e){
						debugger;
						 progressClose();
						 alert('读取服务出错,请确认动态服务URL无误');
						 return isValid;
					}
       			 }else{
       				 return isValid;
       			 }
			}
			 
	});
</script>

<script type="text/javascript">
/*      $(function() {
        $('#roleAddForm').form({
            url : '${path }/serverController/add',
            onSubmit : function() {
                progressLoad();
                var isValid = $(this).form('validate');
                if (!isValid) {
                    progressClose();
                }
                return isValid;
            },
            success : function(result) {
                progressClose();
                result = $.parseJSON(result);
                if (result.success) {
                    parent.$.modalDialog.openner_dataGrid.datagrid('reload');//之所以能在这里调用到parent.$.modalDialog.openner_dataGrid这个对象，是因为user.jsp页面预定义好了
                  
                    // 赋值到
                    
                    parent.$.modalDialog.handler.dialog('close');
                } else {
                    parent.$.messager.alert('错误', result.msg, 'error');
                }
            }
        });
        
        debugger;
        $('#flag').change(function(){
        	alert(21)
        })
        
    });*/
	
</script>



<div class="easyui-layout" data-options="fit:true,border:false" >
    <div data-options="region:'center',border:false" title="" style="overflow: hidden;padding: 3px;" >
        <form id="roleAddForm" method="post">
            <table class="grid">
                <tr>
                    <td>名称</td>
                    <td><input name="name" id="name" type="text" placeholder="请输入服务名称" class="easyui-validatebox span2" data-options="required:true" value="">
                    </td>
                </tr>
                <tr>
                    <td>状态</td>
                    <td >
                        <select id="status" name="status" class="easyui-combobox" data-options="width:140,height:29,editable:false,panelHeight:'auto'">
                                    <option value="1">可用</option>
                                    <option value="2">停用</option>
                        </select>
                    </td>
                </tr>
                
                <tr>
                    <td>动态服务URL</td>
                    <td><input name="dynamicsUrl" id="dynamicsUrl" data-dojo-attach-point="dynamicsUrl" type="text" placeholder="请输入URL" class="easyui-validatebox span2" data-options="required:true " style="width:350px" value=""></td>
                </tr>
                
                <tr>
                    <td>要素服务URL</td>
                    <td><input name="featureUrl" type="text" placeholder="请输入URL" class="easyui-validatebox span2" data-options="required:true " style="width:350px" value=""></td>
                </tr>
                
                <tr>
                    <td>备注</td>
                    <td colspan="5"><textarea name="description" id="description" rows="" cols="" style="width: 350px;height: 50px"></textarea></td>
                </tr>
            </table>
        </form>
    </div>
</div>